package org.gap.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.gap.dao.CaractPPDAO;
import org.gap.dao.TiersDAO;

import org.gap.domain.CaractPP;
import org.gap.domain.Tiers;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for CaractPP entities
 * 
 */

@Service("CaractPPService")
@Transactional
public class CaractPPServiceImpl implements CaractPPService {

	/**
	 * DAO injected by Spring that manages CaractPP entities
	 * 
	 */
	@Autowired
	private CaractPPDAO caractPPDAO;

	/**
	 * DAO injected by Spring that manages Tiers entities
	 * 
	 */
	@Autowired
	private TiersDAO tiersDAO;

	/**
	 * Instantiates a new CaractPPServiceImpl.
	 *
	 */
	public CaractPPServiceImpl() {
	}

	/**
	 * Save an existing CaractPP entity
	 * 
	 */
	@Transactional
	public void saveCaractPP(CaractPP caractpp) {
		CaractPP existingCaractPP = caractPPDAO.findCaractPPByPrimaryKey(caractpp.getCppTiers());

		if (existingCaractPP != null) {
			if (existingCaractPP != caractpp) {
				existingCaractPP.setCppTiers(caractpp.getCppTiers());
				existingCaractPP.setCppSexe(caractpp.getCppSexe());
				existingCaractPP.setCppQualite(caractpp.getCppQualite());
				existingCaractPP.setCppNomPers(caractpp.getCppNomPers());
				existingCaractPP.setCppPrenomPers(caractpp.getCppPrenomPers());
				existingCaractPP.setCppNomJeuneFille(caractpp.getCppNomJeuneFille());
				existingCaractPP.setCppDateNaissance(caractpp.getCppDateNaissance());
				existingCaractPP.setCppCsp(caractpp.getCppCsp());
				existingCaractPP.setCppRegimeSocial(caractpp.getCppRegimeSocial());
				existingCaractPP.setCppCodeProfession(caractpp.getCppCodeProfession());
				existingCaractPP.setCppSituationFamille(caractpp.getCppSituationFamille());
				existingCaractPP.setCppNombreEnfants(caractpp.getCppNombreEnfants());
				existingCaractPP.setCppNif(caractpp.getCppNif());
				existingCaractPP.setCppDateSituation(caractpp.getCppDateSituation());
				existingCaractPP.setCppDateEtatPhysique(caractpp.getCppDateEtatPhysique());
				existingCaractPP.setCppEtatPhysique(caractpp.getCppEtatPhysique());
			}
			caractpp = caractPPDAO.store(existingCaractPP);
		} else {
			caractpp = caractPPDAO.store(caractpp);
		}
		caractPPDAO.flush();
	}

	/**
	 */
	@Transactional
	public CaractPP findCaractPPByPrimaryKey(Integer cppTiers) {
		return caractPPDAO.findCaractPPByPrimaryKey(cppTiers);
	}

	/**
	 * Save an existing Tiers entity
	 * 
	 */
	@Transactional
	public CaractPP saveCaractPPTiers(Integer cppTiers, Tiers related_tiers) {
		CaractPP caractpp = caractPPDAO.findCaractPPByPrimaryKey(cppTiers, -1, -1);
		Tiers existingtiers = tiersDAO.findTiersByPrimaryKey(related_tiers.getTieTiers());

		// copy into the existing record to preserve existing relationships
		if (existingtiers != null) {
			existingtiers.setTieTiers(related_tiers.getTieTiers());
			existingtiers.setTieIdentClient(related_tiers.getTieIdentClient());
			existingtiers.setTieTypeTiers(related_tiers.getTieTypeTiers());
			existingtiers.setTieLibExterne(related_tiers.getTieLibExterne());
			existingtiers.setTieNationalite(related_tiers.getTieNationalite());
			existingtiers.setTieTelFix(related_tiers.getTieTelFix());
			existingtiers.setTieTelBureau(related_tiers.getTieTelBureau());
			existingtiers.setTieTelPortable(related_tiers.getTieTelPortable());
			existingtiers.setTieFax(related_tiers.getTieFax());
			existingtiers.setTieEMail(related_tiers.getTieEMail());
			existingtiers.setTieDateCreation(related_tiers.getTieDateCreation());
			existingtiers.setTieUserCreation(related_tiers.getTieUserCreation());
			related_tiers = existingtiers;
		} else {
			related_tiers = tiersDAO.store(related_tiers);
			tiersDAO.flush();
		}

		caractpp.setTiers(related_tiers);
		caractpp = caractPPDAO.store(caractpp);
		caractPPDAO.flush();

		related_tiers = tiersDAO.store(related_tiers);
		tiersDAO.flush();

		return caractpp;
	}

	/**
	 * Load an existing CaractPP entity
	 * 
	 */
	@Transactional
	public Set<CaractPP> loadCaractPPs() {
		return caractPPDAO.findAllCaractPPs();
	}
	
	
	/***
	 * Return a list of CaractPP with the same name
	 * 
	 */
	@Transactional
	public Set<CaractPP> loadCaractPPsByName(String name) {
		
		return caractPPDAO.findCaractPPByCppNomPersContaining(name);
	}

	/**
	 * Delete an existing CaractPP entity
	 * 
	 */
	@Transactional
	public void deleteCaractPP(CaractPP caractpp) {
		caractPPDAO.remove(caractpp);
		caractPPDAO.flush();
	}

	/**
	 * Delete an existing Tiers entity
	 * 
	 */
	@Transactional
	public CaractPP deleteCaractPPTiers(Integer caractpp_cppTiers, Integer related_tiers_tieTiers) {
		CaractPP caractpp = caractPPDAO.findCaractPPByPrimaryKey(caractpp_cppTiers, -1, -1);
		Tiers related_tiers = tiersDAO.findTiersByPrimaryKey(related_tiers_tieTiers, -1, -1);

		caractpp.setTiers(null);
		caractpp = caractPPDAO.store(caractpp);
		caractPPDAO.flush();

		related_tiers = tiersDAO.store(related_tiers);
		tiersDAO.flush();

		tiersDAO.remove(related_tiers);
		tiersDAO.flush();

		return caractpp;
	}

	/**
	 * Return all CaractPP entity
	 * 
	 */
	@Transactional
	public List<CaractPP> findAllCaractPPs(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<CaractPP>(caractPPDAO.findAllCaractPPs(startResult, maxRows));
	}

	/**
	 * Return a count of all CaractPP entity
	 * 
	 */
	@Transactional
	public Integer countCaractPPs() {
		return ((Long) caractPPDAO.createQuerySingleResult("select count(o) from CaractPP o").getSingleResult()).intValue();
	}

	public Set<String> loadNamePers() {
		// TODO Auto-generated method stub
		return caractPPDAO.findNomPers();
	}
	

}
